package cn.sms.basic.util;

import cn.sms.user.domain.Logininfo;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * 用请求获取Logininfo信息
 */
public class LoginContext {
    public static RedisTemplate redisTemplate;

    public static Logininfo getLoginInfo(HttpServletRequest request){
        String token = request.getHeader("token");
        if(token == null){
            return null;
        }
        WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
        RedisTemplate redisTemplate = (RedisTemplate) webApplicationContext.getBean("redisTemplate");
        Object ligininfo = redisTemplate.opsForValue().get(token);
        if(ligininfo == null){
            return null;
        }
        return (Logininfo)ligininfo;
    }
}
